package org.smart4j.framework.helper;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.framework.bean.FormParam;
import org.smart4j.framework.bean.ParamNew;
import org.smart4j.framework.util.CodecUtil;
import org.smart4j.framework.util.StreamUtil;
import org.smart4j.framework.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * 封装非文件上传请求的helper
 */
public final class RequestHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(RequestHelper.class);

    /**
     * 创建请求参数对象
     * @param request
     * @return
     */
    public static ParamNew createParam(HttpServletRequest request){

        ArrayList<FormParam> formParamList = new ArrayList<>();
        formParamList.addAll(parseParameterName(request));
        formParamList.addAll(parseInputStream(request));
        return new ParamNew(formParamList);
    }

    public static List<FormParam> parseParameterName(HttpServletRequest request){
        ArrayList<FormParam> formParams = new ArrayList<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String paramName = parameterNames.nextElement();
            String[] parameterValues = request.getParameterValues(paramName);
            String paramValue = null;
            if (parameterValues != null){
                if (parameterValues.length == 1){
                    paramValue = parameterValues[0];
                }else{
                    StringBuffer sb = new StringBuffer("");
                    for (int i=0;i<parameterValues.length;i++){
                        sb.append(parameterValues[0]);
                        if (i != parameterValues.length-1){
                            sb.append(StringUtil.SEPERATOR);
                        }
                    }
                    paramValue= sb.toString();
                }
            }
            formParams.add(new FormParam(paramName,paramValue));
        }
        return formParams;
    }



    public static List<FormParam> parseInputStream(HttpServletRequest request){
        List<FormParam> formParams = new ArrayList<>();
        try {
            String  body = CodecUtil.decodeURL(StreamUtil.getString(request.getInputStream()));
            if (StringUtils.isNotEmpty(body)){
                String[] params = body.split("&");
                if (ArrayUtils.isNotEmpty(params)){
                    for (String param : params){
                        String[] array = param.split("=");
                        if (ArrayUtils.isNotEmpty(array) && array.length == 2){
                            String paramName = array[0];
                            String paramValue = array[1];
                            formParams.add(new FormParam(paramName,paramValue));
                        }
                    }
                }
            }
        } catch (IOException e) {
            LOGGER.error("create formParam failure",e);
            throw  new RuntimeException(e);
        }

        return formParams;
    }
}
